package cn.pangza.controller;

import cn.pangza.common.exception.BusinessException;
import cn.pangza.common.mybatisplus.entity.PageRequest;
import cn.pangza.common.mybatisplus.entity.PageResult;
import cn.pangza.common.utils.ResponseData;
import cn.pangza.user.entity.dto.UserRoleDto;
import cn.pangza.user.entity.po.SystemUserPo;
import cn.pangza.user.entity.request.LoginRequest;
import cn.pangza.user.entity.request.SystemUserPageRequest;
import cn.pangza.user.entity.request.UpdateUserRequest;
import cn.pangza.user.entity.vo.GetUserInfoVo;
import cn.pangza.user.service.SystemUserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/system_user")
public class SystemUserController {

    @Autowired
    SystemUserService systemUserService;

    @PreAuthorize("@ss.hasAnyPermission('system:user:list') or @ss.hasAnyRole('ROLE_ADMIN')")
    @PostMapping("/page")
    public ResponseData<PageResult<SystemUserPo>> getSystemUserPage(@RequestBody PageRequest<SystemUserPageRequest> pageRequest) {
        return ResponseData.success(systemUserService.getSystemUserPage(pageRequest));
    }

    @PostMapping
    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN') or @ss.hasAnyPermission('system:user:create')")
    public ResponseData createSystemUser(@Validated @RequestBody LoginRequest loginRequest, BindingResult bindingResult) throws BusinessException {
        Boolean isCreate = systemUserService.createSystemUser(loginRequest);
        if(isCreate) return ResponseData.success();
        else return ResponseData.error("创建失败");
    }

    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN') or @ss.hasAnyPermission('system:user:read')")
    public ResponseData<GetUserInfoVo> getUser(@PathVariable("id") Integer id) {
        if(ObjectUtils.isEmpty(id)) return ResponseData.paramError("未知的用户编号");
        return ResponseData.success(systemUserService.getUser(id));
    }

    @PutMapping
    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN') or @ss.hasAnyPermission('system:user:update')")
    public ResponseData updateSystemUser(@Validated @RequestBody UpdateUserRequest updateUserRequest, BindingResult bindingResult) throws BusinessException {
        Boolean isCreate = systemUserService.updateSystemUser(updateUserRequest);
        if(isCreate) return ResponseData.success();
        else return ResponseData.error("创建失败");
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN') or @ss.hasAnyPermission('system:user:delete')")
    public ResponseData deleteUser(@PathVariable("id") Integer id) {
        Boolean result = systemUserService.deleteSystemUser(id);
        if(result) {
            return ResponseData.success();
        } else {
             return ResponseData.error(null);
        }
    }

    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN')")
    @PostMapping("/user_role")
    public ResponseData updateUserRole(@RequestBody UserRoleDto userRoleDto) {
        systemUserService.updateUserRole(userRoleDto);
        return ResponseData.success();
    }

    @PreAuthorize("@ss.hasAnyRole('ROLE_ADMIN')")
    @GetMapping("/user_role/{id}")
    public ResponseData<List<Integer>> getRoleIdsByUserId(@PathVariable("id") Integer id) {
        return ResponseData.success(systemUserService.getRoleIdsByUserId(id));
    }

}
